<html>
<head>
  <meta HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=ISO-8859-1">
  <title>mlcgmm.m</title>
<link rel="stylesheet" type="text/css" href="../../../m-syntax.css">
</head>
<body>
<code>
<span class=defun_kw>function</span>&nbsp;<span class=defun_out>model</span>=<span class=defun_name>mlcgmm</span>(<span class=defun_in>data,cov_type</span>)<br>
<span class=h1>%&nbsp;MLCGMM&nbsp;Maximal&nbsp;Likelihood&nbsp;estimation&nbsp;of&nbsp;Gaussian&nbsp;mixture&nbsp;model.</span><br>
<span class=help>%&nbsp;</span><br>
<span class=help>%&nbsp;<span class=help_field>Synopsis:</span></span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;mlcgmm(X)</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;mlcgmm(X,cov_type)</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;mlcgmm(data)</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;mlcgmm(data,cov_type)</span><br>
<span class=help>%&nbsp;</span><br>
<span class=help>%&nbsp;<span class=help_field>Description:</span></span><br>
<span class=help>%&nbsp;&nbsp;It&nbsp;computes&nbsp;Maximum&nbsp;Likelihood&nbsp;estimation&nbsp;of&nbsp;parameters</span><br>
<span class=help>%&nbsp;&nbsp;of&nbsp;Gaussian&nbsp;mixture&nbsp;model&nbsp;for&nbsp;given&nbsp;labeled&nbsp;data&nbsp;sample</span><br>
<span class=help>%&nbsp;&nbsp;(complete&nbsp;data).</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;mlcgmm(X)&nbsp;computes&nbsp;parameters&nbsp;(model.Mean,model.Cov)</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;of&nbsp;a&nbsp;single&nbsp;Gaussian&nbsp;distribution&nbsp;for&nbsp;given&nbsp;sample&nbsp;of&nbsp;column&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;vectors&nbsp;X&nbsp;(all&nbsp;labels&nbsp;are&nbsp;assumed&nbsp;to&nbsp;be&nbsp;1).</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;mlcgmm(X,cov_type)&nbsp;specifies&nbsp;shape&nbsp;of&nbsp;covariance&nbsp;matrix:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;cov_type&nbsp;=&nbsp;'full'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;full&nbsp;covariance&nbsp;matrix&nbsp;(default)</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;cov_type&nbsp;=&nbsp;'diag'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;diagonal&nbsp;covarinace&nbsp;matrix</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;cov_type&nbsp;=&nbsp;'spherical'&nbsp;spherical&nbsp;covariance&nbsp;matrix</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;mlcgmm(data)&nbsp;computes&nbsp;parameters&nbsp;of&nbsp;a&nbsp;Gaussian&nbsp;mixture&nbsp;model</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;from&nbsp;a&nbsp;given&nbsp;labeled&nbsp;data&nbsp;sample</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data.X&nbsp;...&nbsp;samples,</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;data.y&nbsp;..&nbsp;labels.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;It&nbsp;estimates&nbsp;parameters&nbsp;of&nbsp;ncomp=max(data.y)&nbsp;Gaussians&nbsp;and</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;a&nbsp;priory&nbsp;probabilities&nbsp;Prior&nbsp;[1&nbsp;x&nbsp;ncomp]&nbsp;using&nbsp;Maximum-Likelihood&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;principle.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Input:</span></span><br>
<span class=help>%&nbsp;&nbsp;X&nbsp;[dim&nbsp;x&nbsp;num_data]&nbsp;Data&nbsp;sample.</span><br>
<span class=help>%&nbsp;&nbsp;data.X&nbsp;[dim&nbsp;x&nbsp;num_data]&nbsp;Data&nbsp;sample.</span><br>
<span class=help>%&nbsp;&nbsp;data.y&nbsp;[1&nbsp;x&nbsp;num_data]&nbsp;Data&nbsp;labels.</span><br>
<span class=help>%&nbsp;&nbsp;cov_type&nbsp;[string]&nbsp;Type&nbsp;of&nbsp;covariacne&nbsp;matrix&nbsp;(see&nbsp;above).</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Output:</span></span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;[struct]&nbsp;Estimated&nbsp;Gaussian&nbsp;mixture&nbsp;model:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.Mean&nbsp;[dim&nbsp;x&nbsp;ncomp]&nbsp;Mean&nbsp;vectors.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.Cov&nbsp;[dim&nbsp;x&nbsp;dim&nbsp;x&nbsp;ncomp]&nbsp;Covariance&nbsp;matrices.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.Prior&nbsp;[1&nbsp;x&nbsp;ncomp]&nbsp;Estimated&nbsp;a&nbsp;priory&nbsp;probabilities.</span><br>
<span class=help>%&nbsp;&nbsp;</span><br>
<span class=help>%&nbsp;<span class=help_field>Example:</span></span><br>
<span class=help>%&nbsp;&nbsp;data&nbsp;=&nbsp;load('riply_trn');</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;mlcgmm(&nbsp;data&nbsp;);</span><br>
<span class=help>%&nbsp;&nbsp;figure;&nbsp;hold&nbsp;on;&nbsp;ppatterns(data);&nbsp;pgauss(&nbsp;model&nbsp;);</span><br>
<span class=help>%&nbsp;&nbsp;figure;&nbsp;hold&nbsp;on;&nbsp;ppatterns(data);&nbsp;pgmm(&nbsp;model&nbsp;);</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;See&nbsp;also&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;EMGMM,&nbsp;MMGAUSS,&nbsp;PDFGMM.</span><br>
<span class=help>%</span><br>
<hr>
<span class=help1>%&nbsp;<span class=help1_field>About:</span>&nbsp;Statistical&nbsp;Pattern&nbsp;Recognition&nbsp;Toolbox</span><br>
<span class=help1>%&nbsp;(C)&nbsp;1999-2003,&nbsp;Written&nbsp;by&nbsp;Vojtech&nbsp;Franc&nbsp;and&nbsp;Vaclav&nbsp;Hlavac</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.cvut.cz"&gt;Czech&nbsp;Technical&nbsp;University&nbsp;Prague&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.feld.cvut.cz"&gt;Faculty&nbsp;of&nbsp;Electrical&nbsp;Engineering&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://cmp.felk.cvut.cz"&gt;Center&nbsp;for&nbsp;Machine&nbsp;Perception&lt;/a&gt;</span><br>
<br>
<span class=help1>%&nbsp;<span class=help1_field>Modifications:</span></span><br>
<span class=help1>%&nbsp;17-aug-2004,&nbsp;VF,&nbsp;labels&nbsp;y&nbsp;do&nbsp;not&nbsp;have&nbsp;to&nbsp;form&nbsp;a&nbsp;sequence&nbsp;1,2,...,max_y</span><br>
<span class=help1>%&nbsp;2-may-2004,&nbsp;VF</span><br>
<span class=help1>%&nbsp;29-apr-2004,&nbsp;VF</span><br>
<span class=help1>%&nbsp;19-sep-2003,&nbsp;VF</span><br>
<span class=help1>%&nbsp;27-feb-2003,&nbsp;VF</span><br>
<br>
<hr>
<span class=comment>%&nbsp;processing&nbsp;of&nbsp;&nbsp;inputs</span><br>
data=c2s(data);<br>
<br>
<span class=keyword>if</span>&nbsp;~isstruct(data),<br>
&nbsp;&nbsp;data.X&nbsp;=&nbsp;data;<br>
&nbsp;&nbsp;data.y&nbsp;=&nbsp;ones(1,size(data.X,2));<br>
<span class=keyword>end</span><br>
&nbsp;<br>
<span class=keyword>if</span>&nbsp;<span class=stack>nargin</span>&nbsp;&lt;&nbsp;2,&nbsp;cov_type&nbsp;=&nbsp;<span class=quotes>'full'</span>;&nbsp;<span class=keyword>end</span><br>
<br>
[dim,num_data]&nbsp;=&nbsp;size(data.X);<br>
<br>
labels&nbsp;=&nbsp;unique(data.y);<br>
model.Mean&nbsp;=&nbsp;zeros(dim,length(labels));<br>
model.Cov&nbsp;=&nbsp;zeros(dim,dim,length(labels));<br>
<span class=keyword>for</span>&nbsp;i=1:length(labels),<br>
&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;inx&nbsp;=&nbsp;find(data.y==labels(i));<br>
&nbsp;&nbsp;&nbsp;n&nbsp;=&nbsp;length(inx);<br>
<br>
&nbsp;&nbsp;&nbsp;model.Mean(:,i)&nbsp;=&nbsp;sum(data.X(:,inx),2)/n;<br>
<br>
&nbsp;&nbsp;&nbsp;XC=data.X(:,inx)-model.Mean(:,i)*ones(1,n);<br>
<br>
&nbsp;&nbsp;&nbsp;<span class=keyword>switch</span>&nbsp;cov_type,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=label>case</span>&nbsp;<span class=quotes>'full'</span>,&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model.Cov(:,:,i)&nbsp;=&nbsp;XC*XC'/n;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=label>case</span>&nbsp;<span class=quotes>'diag'</span>,&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model.Cov(:,:,i)&nbsp;=&nbsp;diag(sum(XC.^2,2)/n);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=label>case</span>&nbsp;<span class=quotes>'spherical'</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model.Cov(:,:,i)&nbsp;=&nbsp;eye(dim,dim)*sum(sum(XC.^2))/(n*dim);<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=label>otherwise</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=error>error</span>(<span class=quotes>'Wrong&nbsp;cov_type.'</span>);<br>
&nbsp;&nbsp;&nbsp;<span class=keyword>end</span><br>
&nbsp;&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;model.Prior(i)&nbsp;=&nbsp;n/num_data;<br>
&nbsp;&nbsp;&nbsp;model.y(i)&nbsp;=&nbsp;labels(i);<br>
<span class=keyword>end</span><br>
<br>
model.cov_type&nbsp;=&nbsp;cov_type;<br>
model.fun&nbsp;=&nbsp;<span class=quotes>'pdfgmm'</span>;<br>
<br>
<span class=jump>return</span>;&nbsp;<br>
</code>
